package com.example.departmentService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.departmentService.entity.PerformanceCount;
import com.example.departmentService.entity.dto.InsertFinalPerformanceDto;
import com.example.departmentService.entity.dto.OthersPerformanceDto;
import com.example.departmentService.entity.dto.PageConditions;
import com.example.departmentService.entity.dto.AttendanceDto;
import com.example.departmentService.mapper.PerformanceCountMapper;
import com.example.departmentService.service.IPerformanceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PerformanceServiceImpl extends ServiceImpl<PerformanceCountMapper, PerformanceCount> implements IPerformanceService {

    @Resource
    private PerformanceCountMapper performanceCountMapper;

    @Override
    public Page<PerformanceCount> queryPerformance(PageConditions pageConditions) {
        Page<PerformanceCount> page = new Page<>(pageConditions.getCurrent(), pageConditions.getSize());
        return performanceCountMapper.selectPage(page, null);
    }

    @Override
    public int insertAttendance(AttendanceDto attendanceDto) {
        UpdateWrapper<PerformanceCount> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", attendanceDto.getId()).set("attendance", attendanceDto.getAttendance());
        int update = performanceCountMapper.update(null, wrapper);
        calculatePerformance(attendanceDto.getId());
        return update;
    }

    @Override
    public int insertOthersPerformance(OthersPerformanceDto performanceDto) {
        UpdateWrapper<PerformanceCount> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", performanceDto.getId())
                .set("workSituation", performanceDto.getWorkSituation())
                .set("workEfficiency", performanceDto.getWorkEfficiency())
                .set("developmentPotential", performanceDto.getDevelopmentPotential())
                .set("communicateAbility", performanceDto.getCommunicateAbility());
        int update = performanceCountMapper.update(null, wrapper);
        calculatePerformance(performanceDto.getId());
        return update;
    }

    /**
     * 计算总分
     *
     * @param id 需要计算的id
     */
    private void calculatePerformance(Integer id) {
        QueryWrapper<PerformanceCount> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        PerformanceCount performanceCount = performanceCountMapper.selectOne(wrapper);
        if (performanceCount.getFinalPerformance().equals("") &&
                performanceCount.getWorkSituation() != null &&
                performanceCount.getAttendance() != null) {
            // 计算总评
            double finalScore = performanceCount.getWorkSituation() * 0.5 +
                    performanceCount.getWorkEfficiency() * 0.1 +
                    performanceCount.getDevelopmentPotential() * 0.1 +
                    performanceCount.getCommunicateAbility() * 0.1 +
                    performanceCount.getAttendance() * 0.2;
            UpdateWrapper<PerformanceCount> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", id).set("totalScore", finalScore);
            performanceCountMapper.update(null, updateWrapper);

        }
    }

    /**
     * 计算等第
     *
     * @param startDate 时间区间起
     * @param endDate   时间区间止
     */
    private void insertFinalPerformance(Date startDate, Date endDate) {
        // 查询
        QueryWrapper<PerformanceCount> wrapper = new QueryWrapper<>();
        wrapper.ge("editDate", startDate).le("editDate", endDate);
        List<PerformanceCount> performanceCounts = performanceCountMapper.selectList(wrapper);
        performanceCounts.sort((o1, o2) -> o2.getTotalScore() - o1.getTotalScore());
        // 计算
        int total = performanceCounts.size();
        List<InsertFinalPerformanceDto> newList = new ArrayList<>();
        for (int i = 0; i < Math.floor(total * 0.2); i++) {
            PerformanceCount performanceCount = performanceCounts.remove(0);
            InsertFinalPerformanceDto finalPerformanceDto = new InsertFinalPerformanceDto(performanceCount.getId(), "A");
            newList.add(finalPerformanceDto);
        }
        for (int i = 4; i < Math.floor(total * 0.65); i++) {
            PerformanceCount performanceCount = performanceCounts.remove(0);
            InsertFinalPerformanceDto finalPerformanceDto = new InsertFinalPerformanceDto(performanceCount.getId(), "B");
            newList.add(finalPerformanceDto);
        }
        for (int i = 17; i < Math.floor(total * 0.1); i++) {
            PerformanceCount performanceCount = performanceCounts.remove(0);
            InsertFinalPerformanceDto finalPerformanceDto = new InsertFinalPerformanceDto(performanceCount.getId(), "C");
            newList.add(finalPerformanceDto);
        }
        for (int i = 20; i < performanceCounts.size(); i++) {
            PerformanceCount performanceCount = performanceCounts.remove(0);
            InsertFinalPerformanceDto finalPerformanceDto = new InsertFinalPerformanceDto(performanceCount.getId(), "D");
            newList.add(finalPerformanceDto);
        }
        // 存入数据
        for (InsertFinalPerformanceDto item : newList) {
            UpdateWrapper<PerformanceCount> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", item.getId()).set("finalPerformance", item.getFinalPerformance());
            performanceCountMapper.update(null, updateWrapper);
        }
    }
}
